/**
 * @projectName leetode
 * @package cn.leetcode
 * @className cn.leetcode.t110
 * @copyright Copyright 2020 Thunisoft, Inc All rights reserved.
 */
package cn.leetcode;

/**
 * t110
 * @description
 * @author fei
 * @date 2025/9/10 15:50
 * @version 1.0
 */
/*
给定一个二叉树，判断它是否是 平衡二叉树

示例 1：

输入：root = [3,9,20,null,null,15,7]
输出：true
示例 2：

输入：root = [1,2,2,3,3,null,null,4,4]
输出：false
示例 3：

输入：root = []
输出：true
 */
public class t110 {
    boolean ans = true;
    public boolean isBalanced(TreeNode root) {
        getHeight(root);
        return ans;
    }

    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = getHeight(root.left);
        int right = getHeight(root.right);
        if (Math.abs(left - right) > 1){
            ans = false;
        }
        return Math.max(left, right) + 1;
    }
}
